package DemoText;

/**
 * 最长回文子串
 */
public class LeetCode5 {
    public String longestPalindrome(String s) {
        String str = "";
        int len = s.length();
        for (int left = 0, tmp = 0, max = 0; left < len; left++) {
            for (int right = tmp; right < len; right++) {
                if (s.charAt(left) == s.charAt(right)) {
                    String strtmp = s.substring(left, right + 1);
                    if (isPalindRome(strtmp)) {
                        if (max < right - left + 1) {
                            max = right - left + 1;
                            str = strtmp;
                        }
                        tmp = right;
                    }
                }
            }
        }
        return str;
    }

    public boolean isPalindRome(String str) {
        char[] ch = str.toCharArray();
        int left = 0, right = ch.length - 1;
        while (left < right)
            if (ch[left++] != ch[right--])
                return false;
        return true;
    }
}
